<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%= packageName %>.config;

import tech.jhipster.config.JHipsterConstants;
<%_ if (databaseTypeNeo4j || reactive) { _%>
import java.util.Optional;
import tech.jhipster.config.liquibase.AsyncSpringLiquibase;
<%_ } else { _%>
import tech.jhipster.config.liquibase.SpringLiquibaseUtil;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseDataSource;
import org.springframework.beans.factory.ObjectProvider;
<%_ } _%>
<%_ if (databaseTypeSql && reactive) { _%>
import org.springframework.boot.autoconfigure.r2dbc.R2dbcProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
<%_ } _%>
<%_ if (databaseTypeNeo4j) { _%>
import liquibase.ext.neo4j.database.jdbc.Neo4jDriver;
import org.springframework.boot.autoconfigure.neo4j.Neo4jProperties;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
<%_ } _%>

import javax.sql.DataSource;
import java.util.concurrent.Executor;

import liquibase.integration.spring.SpringLiquibase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

@Configuration
public class LiquibaseConfiguration {

    private static final Logger LOG = LoggerFactory.getLogger(LiquibaseConfiguration.class);

    private final Environment env;

    public LiquibaseConfiguration(Environment env) {
        this.env = env;
    }

    @Bean
    public SpringLiquibase liquibase(
        @Qualifier("taskExecutor") Executor executor,
        LiquibaseProperties liquibaseProperties,
<%_ if (databaseTypeNeo4j) { _%>
        Neo4jProperties dataSourceProperties
<%_ } else if (reactive) { _%>
        R2dbcProperties dataSourceProperties
<%_ } else { _%>
        @LiquibaseDataSource ObjectProvider<DataSource> liquibaseDataSource,
        ObjectProvider<DataSource> dataSource,
        ApplicationProperties applicationProperties,
        DataSourceProperties dataSourceProperties
<%_ } _%>
    ) {

<%_ if (databaseTypeNeo4j || reactive) { _%>
        SpringLiquibase liquibase = new AsyncSpringLiquibase(executor, env);
        liquibase.setDataSource(createLiquibaseDataSource(liquibaseProperties, dataSourceProperties));
<%_ } else { _%>
        SpringLiquibase liquibase;
        if (Boolean.TRUE.equals(applicationProperties.getLiquibase().getAsyncStart())) {
            liquibase = SpringLiquibaseUtil.createAsyncSpringLiquibase(this.env, executor, liquibaseDataSource.getIfAvailable(), liquibaseProperties, dataSource.getIfUnique(), dataSourceProperties);
        } else {
            liquibase = SpringLiquibaseUtil.createSpringLiquibase(liquibaseDataSource.getIfAvailable(), liquibaseProperties, dataSource.getIfUnique(), dataSourceProperties);
        }
<%_ } _%>
        liquibase.setChangeLog("classpath:config/liquibase/master.xml");
        if (!CollectionUtils.isEmpty(liquibaseProperties.getContexts())) {
            liquibase.setContexts(StringUtils.collectionToCommaDelimitedString(liquibaseProperties.getContexts()));
        }
        liquibase.setDefaultSchema(liquibaseProperties.getDefaultSchema());
        liquibase.setLiquibaseSchema(liquibaseProperties.getLiquibaseSchema());
        liquibase.setLiquibaseTablespace(liquibaseProperties.getLiquibaseTablespace());
        liquibase.setDatabaseChangeLogLockTable(liquibaseProperties.getDatabaseChangeLogLockTable());
        liquibase.setDatabaseChangeLogTable(liquibaseProperties.getDatabaseChangeLogTable());
        liquibase.setDropFirst(liquibaseProperties.isDropFirst());
        if (!CollectionUtils.isEmpty(liquibaseProperties.getLabelFilter())) {
            liquibase.setLabelFilter(StringUtils.collectionToCommaDelimitedString(liquibaseProperties.getLabelFilter()));
        }
        liquibase.setChangeLogParameters(liquibaseProperties.getParameters());
        liquibase.setRollbackFile(liquibaseProperties.getRollbackFile());
        liquibase.setTestRollbackOnUpdate(liquibaseProperties.isTestRollbackOnUpdate());
        if (env.matchesProfiles(JHipsterConstants.SPRING_PROFILE_NO_LIQUIBASE)) {
            liquibase.setShouldRun(false);
        } else {
            liquibase.setShouldRun(liquibaseProperties.isEnabled());
            LOG.debug("Configuring Liquibase");
        }
        return liquibase;
    }
<%_ if (databaseTypeNeo4j) { _%>

    public DataSource createLiquibaseDataSource(LiquibaseProperties liquibaseProperties, Neo4jProperties dataSourceProperties) {
        String url = Optional.ofNullable(liquibaseProperties.getUrl()).orElse("jdbc:neo4j:" + dataSourceProperties.getUri().toString());
        String user = Optional.ofNullable(liquibaseProperties.getUser()).orElse(dataSourceProperties.getAuthentication().getUsername());
        String password = Optional.ofNullable(liquibaseProperties.getPassword()).orElse(dataSourceProperties.getAuthentication().getPassword());

        return new SimpleDriverDataSource(new Neo4jDriver(), url, user, password);
    }
<%_ } _%>
<%_ if (databaseTypeSql && reactive) { _%>

    private static DataSource createLiquibaseDataSource(LiquibaseProperties liquibaseProperties, R2dbcProperties dataSourceProperties) {
        String user = Optional.ofNullable(liquibaseProperties.getUser()).orElse(dataSourceProperties.getUsername());
        String password = Optional.ofNullable(liquibaseProperties.getPassword()).orElse(dataSourceProperties.getPassword());

        return DataSourceBuilder.create().url(liquibaseProperties.getUrl()).username(user).password(password).build();
    }
<%_ } _%>
}
